package com.dy.quartz.task;

import com.alibaba.fastjson2.JSON;
import com.dy.common.core.domain.entity.SysUser;
import com.dy.common.utils.DateUtils;
import com.dy.common.utils.StringUtils;
import com.dy.common.utils.cms.AliyunSmsUtils;
import com.dy.system.domain.FreshMedicalMachine;
import com.dy.system.domain.FreshWarningMsgTypeDetail;
import com.dy.system.domain.MedicalInstitution;
import com.dy.system.service.IFreshMedicalMachineService;
import com.dy.system.service.IFreshWarningMsgTypeDetailService;
import com.dy.system.service.IMedicalInstitutionService;
import com.dy.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author anps
 */
@Component("ryTask")
public class RyTask{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IFreshMedicalMachineService freshMedicalMachineService;
    @Autowired
    private IMedicalInstitutionService medicalInstitutionService;
    @Autowired
    private IFreshWarningMsgTypeDetailService freshWarningMsgTypeDetailService;
    @Autowired
    private ISysUserService sysUserService;
    /***
     * 检测检测报告
     */
    public void checkReport(){
        logger.info("检测报告定时任务开始执行。。。");
        //获取当前月份
        int currentMonth = DateUtils.getCurrentMonth();
        //获取本月最后两天的时间戳 如0929
        long lastDayOfMonth = DateUtils.getPerLastDayOfMonth();
        //获取下一个月第一天日期的时间戳
        long firstDayOfMonth = DateUtils.getPerFirstDayOfMonth();
        Integer level = 3;
        if ("1,4,7,10".contains(currentMonth+"")){
            level = 1;
        }else if ("2,5,7,11".contains(currentMonth+"")){
            level = 2;
        }
        long currentTime = new Date().getTime();
        //再时间段内执行
        if (currentTime > lastDayOfMonth && currentMonth < firstDayOfMonth){
            //查询未上传的检测报告的机构
            String session = DateUtils.getSession(new Date());
            String year = DateUtils.parseDateToStr(DateUtils.YYYY,new Date());
            List<MedicalInstitution> institutionList = medicalInstitutionService.
                    getNotUploadedInstitutionList(null,session,year);
            logger.info("未上传检测报告的机构 = {}", JSON.toJSONString(institutionList));
            for (MedicalInstitution m : institutionList) {
                //查询绑定的设备信息
                FreshMedicalMachine machine = freshMedicalMachineService.selectFreshMedicalMachineByInstitutionId(m.getId());
                if (machine == null){
                    continue;
                }
                FreshWarningMsgTypeDetail detail = new FreshWarningMsgTypeDetail();
                detail.setMedicalId(m.getId());
                detail.setWarningType("jiancebaogao");
                detail.setWarningLevel(level);
                detail.setGjType(1);
                detail.setCode(m.getCode());
                detail.setRunningStatus(machine.getRunningStatus());
                detail.setWarningMsg(new StringBuffer("当前季度").append(session).append("未上传检测报告").toString());
                logger.info("检测报告告警信息插入 = {}",JSON.toJSONString(detail));
                freshWarningMsgTypeDetailService.insertFreshWarningMsgTypeDetail(detail);
                //查询手机号
                SysUser sysUser = sysUserService.selectUserByUserName(m.getCreateBy());
                if (sysUser != null && StringUtils.isNotBlank(sysUser.getPhonenumber())){
                    // 发送短信
                    Map<String,Object> param = new HashMap<>();
                    param.put("name",sysUser.getUserName());
                    param.put("number",session);
                    try {
                        AliyunSmsUtils.sendBatchSms("SMS_248825266",sysUser.getPhonenumber(),param);
                    } catch (Exception e) {
                    }
                }
            }
        }









    }






    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i){

    }

    public void ryParams(String params)throws Exception{
        //System.out.println("执行有参方法：" + params);
//        List<FreshMedicalMachine> machineList = freshMedicalMachineService.selectFreshMedicalMachineList(new FreshMedicalMachine());
//        for (FreshMedicalMachine medicalMachine : machineList) {
//            //发送消息
//            Map<String,Object> accessDataMap = new HashMap<>();
//            accessDataMap.put("Unix",System.currentTimeMillis());
//            accessDataMap.put("Version","V1.0");
//            accessDataMap.put("Content","alldata");
//            logger.info("ryNoParams accessDataMap ={}", JSON.toJSONString(accessDataMap));
//            MqttSendClient.publish(false,medicalMachine.getMachineCode()+"12345678/access_data", JSON.toJSONString(accessDataMap));
//            Thread.sleep(300);
//        }
    }

    public void ryNoParams()throws Exception{
        //System.out.println("执行无参方法");
        /*List<FreshMedicalMachine> machineList = freshMedicalMachineService.selectFreshMedicalMachineList(new FreshMedicalMachine());
        for (FreshMedicalMachine medicalMachine : machineList) {
            //发送消息
            Map<String,Object> accessDataMap = new HashMap<>();
            accessDataMap.put("Unix",System.currentTimeMillis());
            accessDataMap.put("Version","V1.0");
            accessDataMap.put("Content","alldata");
            logger.info("ryNoParams accessDataMap ={}", JSON.toJSONString(accessDataMap));
            MqttSendClient.publish(false,medicalMachine.getMachineCode()+"12345678/access_data", JSON.toJSONString(accessDataMap));
            Thread.sleep(300);
        }*/
    }
}
